<?php

namespace App\Admin\Renderable;


use App\Models\SwtMachine;
use App\Models\SwtMerchantMachine;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class MachineRenderable extends LazyRenderable
{
    public function grid(): Grid
    {

        $id = $this->id ?? '';

        $ids = SwtMerchantMachine::where('merchant_id', $id) -> where('is_owner', 1) ->pluck('machine_id');

        return Grid::make(new SwtMachine(), function (Grid $grid) use ($ids){

            if($ids) {
                $grid->model()->whereIn('id', $ids);
            }

            $grid->column('id');

            $grid->column('machine_name','设备名') -> display(function ($value) {
                return $value ?? '-';
            });

            $grid->column('machine_no','设备号');

            $grid->column('run_mode','状态') -> using(['禁用','启用'])->label(['danger','success']);

            $grid->column('source_type','设备来源') -> using(["全部", '大黄鹅', '宝坚'])->label(['info','warning','success']);


            $grid->quickSearch(['machine_no','machine_name']);

            $grid->paginate(5);

            $grid->disableRowSelector(false);

            $grid->rowSelector()->disable(function ($row) use ($ids, $id) {
                if($row->id == $id) {
                    return false;
                }
                return in_array($row->id, $ids);
            });

            $grid->disableActions();
        });
    }
}
